<?php echo $this->render('/public/header'); ?>
<style>
    .jing-product {
        max-height: 100px;
        overflow: hidden;
    }

    .jing-product:hover {
        height: auto;
        max-height: none;
        overflow: auto;
    }
</style>
<script src="https://cdn.bootcss.com/element-ui/1.4.2/index.js"></script>
<link href="https://cdn.bootcss.com/element-ui/1.4.2/theme-default/index.css" rel="stylesheet">
<div class="col-xs-12" id="main">
    <a class="btn btn-success" href="/sell/invoice">添加invoice</a>
    <div style="float: right;margin-bottom: 10px;">
        <p><b>注意事项：</b></p>
        <p><b>一个客户的同几个产品（可能对数量，价格，或增加减少产品项目），都在同一个报价里面修改。</b></p>
        <p><b>一个客户先后对不同的产品进行报价，可以做在不同的报价单里。</b></p>
        <p><b>尽量在整个列表里面可以清楚看见自己对不同客户不同产品的历史报价</b></p>
    </div>
    <br/>
    <br/>
    <form action="">
        <div class="form-inline">
            <div class="form-group">
                <select class="form-control" name="job_number">
                    <option value="0">请选择</option>
                    <?php foreach ($this->params['staffList'] as $v): ?>
                        <option value="<?php echo $v['job_number']; ?>"><?php echo $v['name']; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <input type="text" class="form-control" name="invoice" placeholder="搜索invoice">
            </div>
            <div class="form-group">
                <input type="text" class="form-control" value="<?php echo yii::$app->request->get("customer_name"); ?>"
                       name="customer_name" placeholder="搜索客户名称">
            </div>
            <div class="form-group">
                <input type="checkbox" name="show_invoice"
                       value="1" <?php echo yii::$app->request->get('show_invoice') == 1 ? 'checked' : ''; ?>
                       style="width:30px"/>显示所有无invoice
            </div>
            <button class="btn btn-success">搜索</button>
        </div>
    </form>
    <br/>
    <div class="col-lg-12 col-sm-12 col-md-12 col-xs-12">
        <?php if ($this->params['list']): ?>
            <?php foreach ($this->params['list'] as $v): ?>
                <div class="panel panel-<?php echo ['success', 'primary', 'danger', 'info'][rand(0, 3)]; ?>">
                    <div class="panel-heading"><?php echo $v['date']; ?>
                        <?php foreach ($this->params['staffList'] as $v2): ?>
                            <?php if ($v['job_number'] == $v2['job_number']): ?>
                                <?php echo $v2['name']; ?>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </div>
                    <div class="panel-body">
                        <?php $product = json_decode($v['product_list'], true); ?>
                        <div class="col-lg-2 col-sm-2 col-md-2 col-xs-2"><img style="width:100px;height:100px"
                                                                              src="<?php echo isset($product[0]['img_src'][0]) ? $product[0]['img_src'][0] : '/statics/img/logo2.png'; ?>"/>
                        </div>
                        <div class="col-lg-1 col-sm-1 col-md-1 col-xs-1"><input
                                    onchange="update(<?php echo $v['id']; ?>,'invoice_id',this)" style="width:100%"
                                    type="text" value="<?php echo $v['invoice_id']; ?>"/></div>
                        <div class="col-lg-1 col-sm-1 col-md-1 col-xs-1"><?php echo $v['pi_id']; ?></div>
                        <div class="col-lg-1 col-sm-1 col-md-1 col-xs-1"><?php $customer = json_decode($v['customer_info'], true);
                            echo $customer['buyer']; ?></div>
                        <div class="col-lg-1 col-sm-1 col-md-1 col-xs-1"><?php echo $v['number']; ?></div>
                        <div class="col-lg-2 col-sm-2 col-md-2 col-xs-2">
                            <?php
                            foreach ($product as $v2):
                                echo $v2['item'] . "<br/>";
                            endforeach;
                            ?>
                        </div>
                        <div class="btn-group">
                            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
                                    aria-haspopup="true" aria-expanded="false">
                                查看历史版本<span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu" style="margin-left:25px">
                                <?php if (isset($this->params['oldlist'][$v['id']])): ?>
                                    <?php foreach ($this->params['oldlist'][$v['id']] as $k2 => $v2) : ?>
                                        <li>
                                            <a href="/sell/invoice_edit?id=<?php echo $v2['id']; ?>"><?php echo date('m-d H:i', $v2['create_at']); ?></a>
                                        </li>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </ul>
                        </div>
                        <a href="/sell/invoice_edit?id=<?php echo $v['id']; ?>"
                           class="layui-btn layui-btn-normal layui-btn-small">编辑</a>
                        <?php /*if(!in_array($v['id'],$this->params['pi_arr'])){?>
                <a href="/sell/invoice?id=<?php echo $v['id'];?>" class="layui-btn layui-btn-warm layui-btn-small">添加pi</a>
                <?php }*/ ?>
                        <?php if ($this->params['show_del']): ?>
                            <button class="btn btn-danger" onclick="del(<?php echo $v['id']; ?>)">删除</button>
                        <?php endif; ?>
                        <a href="/sell/download_invoice?id=<?php echo $v['id']; ?>&type=1"
                           class="layui-btn layui-btn-danger layui-btn-small">下载excel</a>
                        <!-- <a href="/sell/download_quote?id=<?php echo $v['id']; ?>&type=2" class="layui-btn layui-btn-danger layui-btn-small">下载pdf</a> -->
                        <?php if ($this->params['show_del']): ?>
                            <button class="btn btn-danger" onclick="generateNum(<?php echo $v['id']; ?>)">生成invoice
                            </button>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
        <div id="page" style="display:inline-block;"></div>
    </div>
    <div id="page"></div>
    <script>
        layui.laypage({
            cont: "page",
            curr:<?php echo $this->params['page']['curr'];?>,
            pages:<?php echo $this->params['page']['page'];?>,
            skin: "#5FB878",
            skip: true,
            jump: function (obj, first) {
                if (!first) {
                    if (window.location.search) {
                        window.location.href = "<?php echo $this->params['page']['url'];?>" + "&p=" + obj.curr;
                    }
                    else {
                        window.location.href = "<?php echo $this->params['page']['url'];?>" + "?p=" + obj.curr;
                    }
                }
            }
        })

        function generateNum(id) {
            layer.confirm('确认生成invoice编号', function () {
                $.post('/api/invoice/generate', {id, id}, function (res) {

                }, 'json')
            })
        }

        function update(id, param, e) {
            const value = e.value;
            layer.confirm('确认提交', {}, function () {
                $.post('/sell/invoice-edit-by-param', {id: id, param: param, value: value}, function (res) {
                    if (res.code == 1) {
                        layer.alert(res.msg);
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000)
                    } else {
                        layer.alert(res.msg);
                    }
                }, 'json')
            })
        }
        <?php if($this->params['show_del']):?>
        function del(id) {
            layer.confirm('确认删除', {}, function () {
                $.post('/sell/del-invoice', {id: id}, function (res) {
                    layer.alert(res.msg);
                    if (res.code == 1) {
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000);
                    }
                }, 'json')
            });
        }

        <?php endif;?>
    </script>
</div>
<?php echo $this->render('/public/footer'); ?>
